package leetcode.code1041;

import java.awt.Point;
import java.util.HashSet;
import java.util.Set;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	int[] x = new int[] { 0, 1, 0, -1 };
	int[] y = new int[] { 1, 0, -1, 0 };

	public boolean isRobotBounded(String instructions) {
		char[] ins = instructions.toCharArray();
		int len = ins.length;
		int[] s = new int[4];// x,y,方向，指令指针
		int cnt1 = this.start(len, ins, s);
		s[3] = 0;
		int cnt2 = this.start(len, ins, s);
		return cnt2 == cnt1 && cnt1 != 0;
	}

	private int start(int len, char[] ins, int[] s) {
		int cnt = 0;
		while (s[3] < len << 2) {
			char c = ins[s[3] % len];
			if (c == 'G') {
				s[0] += x[s[2]];
				s[1] += y[s[2]];
			} else if (c == 'L') {
				s[2] = (s[2] + 4 - 1) % 4;
			} else {
				s[2] = (s[2] + 1) % 4;
			}
			if (s[0] == 0 && s[1] == 0 && s[2] == 0) {
				cnt++;
			}
			s[3]++;
		}
		return cnt;
	}

	@Override
	public void debug4() {
		H.compare(true, this.isRobotBounded("GGLLGG"));
		H.compare(false, this.isRobotBounded("GG"));
		H.compare(true, this.isRobotBounded("GL"));
		H.compare(false, this.isRobotBounded("GLGLGGLGL"));
		H.compare(false, this.isRobotBounded("LLLRLLLRLLGLLGGRGLLLGGLRRRRRGLRLRLRLGGRGRGRLLLLLLGLLRLGLGLRLGGGRR"));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
